﻿using System;

namespace _08.FindSequenceWithMaximalSum
{
    class FindSequenceWithMaximalSum
    {
        static void Main()
        {
            Console.Write("Enter lenght of array N=");
            int N = int.Parse(Console.ReadLine());
            int[] array = new int[N];
            Console.WriteLine("Enter elements of array.The elements must be integer numbers.");
            for (int i = 0; i < N; i++)
            {
                array[i] = int.Parse(Console.ReadLine());
            }
            int startIndex = 0;
            int endIndex = 0;
            int tempIndex = 0;
            int sum = 0;
            int result = int.MinValue;
            for (int i = 0; i < N; i++)
            {
                sum += array[i];
                if (sum > result)
                {
                    result = sum;
                    startIndex = tempIndex;
                    endIndex = i;
                }
               if(sum<0)
                {
                    sum = 0;
                    tempIndex = i + 1;
                }
            }
            Console.WriteLine("The sequence of maximal sum is:");
            for (int i = startIndex; i <= endIndex; i++)
            {
                Console.Write(array[i]+" ");
            }
        }
    }
}
